package com.zrrd.aaa;

/**
 * @author WanJl
 * @version 1.0
 * @title MultiThreadExample
 * @description
 * @create 2024/8/22
 */

public class MultiThreadExample implements Runnable {

    private String threadName;

    public MultiThreadExample(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 1100; i++) {
            System.out.println(threadName + " - " + i);
            try {
                // 每输出1个整数，当前线程睡眠300毫秒
                Thread.sleep(300);
            } catch (InterruptedException e) {
                System.out.println(threadName + " interrupted.");
            }
        }
    }

    public static void main(String[] args) {
        // 创建线程类对象
        MultiThreadExample runnable1 = new MultiThreadExample("Thread 1");
        MultiThreadExample runnable2 = new MultiThreadExample("Thread 2");
        MultiThreadExample runnable3 = new MultiThreadExample("Thread 3");

        // 启动线程 t1, t2, t3
        Thread t1 = new Thread(runnable1);
        Thread t2 = new Thread(runnable2);
        Thread t3 = new Thread(runnable3);

        // 设置线程优先级
        t1.setPriority(Thread.MIN_PRIORITY); // 优先级 1
        t2.setPriority(Thread.MAX_PRIORITY); // 优先级 10
        t3.setPriority(Thread.NORM_PRIORITY); // 优先级 5

        t1.start();
        t2.start();
        t3.start();
    }
}